package com.jay.tools.utils;

import android.util.Log;

/**
 * author: tanjianpei
 * date: 2018/8/3 003 11:15
 * <<<<<<< HEAD
 * <p>
 * =======
 * <p>
 * >>>>>>> 39a56ec965c66b49b2038012bfecbf629bb03341
 * description: 长日志
 */

@SuppressWarnings("unused")
public class LogUtil {

    private static boolean IS_DEBUG = true;

    private static final int LEVEL_I = 0;
    private static final int LEVEL_D = 1;
    private static final int LEVEL_E = 2;
    private static final int LEVEL_V = 3;
    private static final int LEVEL_W = 4;

    private static final int LOG_MAX_LENGTH = 2000; //单次日志输出的最大长度

    public LogUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void initLog(boolean isDebug) {
        IS_DEBUG = isDebug;
    }

    public static void i(String tag, String msg) {
        logLong(LEVEL_I, tag, msg);
    }

    public static void d(String tag, String msg) {
        logLong(LEVEL_D, tag, msg);
    }

    public static void e(String tag, String msg) {
        logLong(LEVEL_E, tag, msg);
    }

    public static void v(String tag, String msg) {
        logLong(LEVEL_V, tag, msg);
    }

    public static void w(String tag, String msg) {
        logLong(LEVEL_W, tag, msg);
    }

    /**
     * 打印超长日志
     */
    private static void logLong(int level, String tag, String msg) {
        if (!IS_DEBUG) {
            return;
        }
        int strLength = msg.length();
        int start = 0;
        int end = LOG_MAX_LENGTH;
        for (int i = 0; i < 100; i++) {
            if (strLength > end) {
                logByLevel(level, tag, msg.substring(start, end));
                start = end;
                end = end + LOG_MAX_LENGTH;
            } else {
                logByLevel(level, tag, msg.substring(start, strLength));
                break;
            }
        }
    }

    private static void logByLevel(int level, String tag, String msg) {
        switch (level) {
            case LEVEL_I:
                Log.i(tag, msg);
                break;
            case LEVEL_D:
                Log.d(tag, msg);
                break;
            case LEVEL_E:
                Log.e(tag, msg);
                break;
            case LEVEL_V:
                Log.v(tag, msg);
                break;
            case LEVEL_W:
                Log.w(tag, msg);
                break;
        }
    }


}
